<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class AddColumnProjectIdInReviewRequestTable extends AbstractMigration
{
    public function up()
    {
        $this->table('review_request')
            ->addColumn('project_id', 'integer', [
                'signed' => false,
                'default' => 0,
                'limit' => 11,
                'comment' => '项目id',
            ])->save();
        $this->execute("update review_request rr 
    join version v on v.id = rr.link_id and rr.module_id = 54
 join project p on p.id = v.project_id 
 set rr.project_id = v.project_id
 WHERE rr.project_id = 0
 and rr.tenant_id = v.tenant_id");
        $this->execute("update review_request rr join version v on v.id = rr.link_id and rr.module_id = 54
 join project p on p.id = v.project_id 
 join order_task_list otl on otl.real_task_id = v.link_id and otl.`status` = 'default'
 join `order` o on o.id = otl.order_id
 set rr.project_id = o.project_id
 WHERE rr.project_id = 0
 and rr.tenant_id <> v.tenant_id");

    }

    public function down()
    {
        $this->table('review_request')
            ->removeColumn('project_id')
            ->save();
    }
}
